package com.ti.shangou.service.impl;

import com.ti.shangou.dao.PermissionDao;
import com.ti.shangou.dao.RoleDao;
import com.ti.shangou.dao.UserDao;
import com.ti.shangou.pojo.dto.ResponseDTO;
import com.ti.shangou.pojo.entity.Permission;
import com.ti.shangou.pojo.entity.Role;
import com.ti.shangou.pojo.query.PermissionQuery;
import com.ti.shangou.pojo.vo.PermissionVO;
import com.ti.shangou.pojo.vo.RoleVO;
import com.ti.shangou.service.BaseService;
import com.ti.shangou.service.PermissionService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 作者：田宝山
 * 日期：2020/5/30
 */
@Service
public class PermissionServiceImpl implements PermissionService, BaseService {
    @Resource
    PermissionDao permissionDao;
    @Resource
    RoleDao roleDao;
    @Resource
    UserDao userDao;


    @Override
    public List<PermissionVO> selectPermissionsList(PermissionQuery permissionQuery) {
        return permissionDao.selectPermissionsList(permissionQuery);
    }

    @Override
    public Integer selectPermissionsCount() {
        return permissionDao.selectPermissionsCount();
    }

    // 根据角色集合查权限集合
    public List<PermissionVO> selectPermissionsByRoles(List<RoleVO> roles) {
//        List permissionVOS = new ArrayList();
//        for (RoleVO roleVO:roles) {
//            String permissions = roleVO.getPermissions();
//            String[] split = permissions.split(",");
//            List<PermissionVO> list = permissionDao.selectPermissionsByRoles(split);
//            permissionVOS.add(list);
//        }
//        return permissionVOS;
        Set<String>paramSet = new TreeSet<>();
        for (RoleVO roleVO:roles) {
            String permissions = roleVO.getPermissions();
            String[] split = {};
            if (permissions!=null&&permissions.length()>0){
                split = permissions.split(",");
            }
            Collections.addAll(paramSet,split);
        }
        List<PermissionVO> permissionVOS = permissionDao.selectPermissionsByRoles(paramSet);
        return permissionVOS;
    }

    public List<PermissionVO> selectPermissionBySingle(RoleVO roleVO){
        if (roleVO==null)
            return null;
        Integer roleId = roleVO.getRoleId();
        RoleVO role = roleDao.selectRoleById(roleId);
        if (role.getPermissions()==null&&role.getPermissions()=="")
            return null;
        List<PermissionVO> list = new ArrayList<>();
        if (role.getPermissions().length()>0){
            list = permissionDao.selectPermissionsByRole(role);
        }
        return list;
    }

    public List<PermissionVO> selectHisPermissionByRoles(List<RoleVO> roles) {
        List<PermissionVO> list = new ArrayList<>();
//        // 第一种 ：
//        for (RoleVO role : roles) {
//            List<PermissionVO> permissionVOS = permissionDao.selectPermissionsByIds(role.getPermissions());
//            list.addAll(permissionVOS);
////            Collections.addAll(list, permissionVOS);// 这里有个把两个集合加入到一个集合里边去
//        }
        // 断言工具
//        Assert.notNull(roles, "传递的集合为null");
        if (!CollectionUtils.isEmpty(roles)) {
            Set<String> paramSet = new TreeSet<>();// 查询参数集合
            for (RoleVO role : roles) {// 在内存之中进行的。效率基本最高的
                String psIds = role.getPermissions();// 1,2,34
                if (!StringUtils.isEmpty(psIds)) {
                    Collections.addAll(paramSet, psIds.split(","));
                }
            }
            if (!CollectionUtils.isEmpty(paramSet)) {
                list = permissionDao.selectPermissionsByRoles(paramSet);
            }
        }
        return list;
    }

    public ResponseDTO editPermission(Permission permission){
        return ResponseDTO.get(permissionDao.updateByPrimaryKeySelective(permission)==1);
    }

    public boolean deletePermissionsById(List<PermissionVO> permissionVOS){
        return permissionDao.deletePermissions(permissionVOS)==permissionVOS.size();
    }

    // 权限添加方法
    public ResponseDTO addPermissionToRole(RoleVO role){
        // 传入的权限集合
        List<PermissionVO> permissionVOS = role.getPermissionVOS();
        // 查询到以前的权限集合
        List<PermissionVO> permissionList = selectPermissionBySingle(role);
        // 组合集合
        permissionList.addAll(permissionVOS);

        TreeSet<Integer> set = new TreeSet<>();
        // 遍历组合的集合，放入set
        for (PermissionVO permissionVO:permissionList) {
            set.add(permissionVO.getPermissionId());
        }
        // 集合转为字符串
        String permissions = collectionToStr(set);
        Role r = new Role();
        r.setPermissions(permissions);
        r.setRoleId(role.getRoleId());

        roleDao.updateByPrimaryKeySelective(r);
        return ResponseDTO.get(roleDao.updateByPrimaryKeySelective(r)==1);
    }


    // 权限移除方法
    public Object removePermissionFromRole(RoleVO roleVO){
        RoleVO oldVo = roleDao.selectRoleById(roleVO.getRoleId());
        List<PermissionVO> oldPermissions = permissionDao.selectPermissionsByRole(oldVo);
        List<PermissionVO> transPermissions = roleVO.getPermissionVOS();
        // 不能直接用removeAll()方法移除，因为里面对象不一样
        // 转变为intger的集合就能移除
        TreeSet<Integer> tranCollect = transPermissions.stream().map(PermissionVO::getPermissionId).collect(Collectors.toCollection(TreeSet::new));
        TreeSet<Integer> oldCollect = oldPermissions.stream().map(PermissionVO::getPermissionId).collect(Collectors.toCollection(TreeSet::new));
        if (!StringUtils.isEmpty(oldCollect)){
            oldCollect.removeAll(tranCollect);
        }
        // 集合转字符串
        String permissions = collectionToStr(oldCollect);
        Role upRole = new Role();
        upRole.setRoleId(roleVO.getRoleId());
        upRole.setPermissions(permissions);
        return ResponseDTO.get(roleDao.updateByPrimaryKeySelective(upRole)==1);
    }
}
